package com.zadtek.shopmall.activities;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.zadtek.shopmall.R;
import com.zadtek.shopmall.adapter.MyAdapter;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.utils.SPUtil;
import com.zadtek.shopmall.utils.ToastUtil;
import com.zadtek.shopmall.view.NoScrollView;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SearchActivity extends AbsActivity implements View.OnClickListener {

    private ImageView ivback,ivSearch,ivDeleteRecentSearch;
    private TextView tvSearchType,noSearch;
    private EditText etSearch;
    private NoScrollView gvHotContainer,gvRecentContainer;

    private MyAdapter hotSearchAdapter;
    private MyAdapter recentSearchAdapter;

    private ArrayList<String> hotSearchList = new ArrayList<>();
    private ArrayList<String> recentSearchList = new ArrayList<>();

    private int searchType = 1;//1商品 2店铺 默认商品

    private final static String RECENT_SEARCH_HISTORY = "recent_search_history";
    @Override
    public int setView() {
        return R.layout.activity_search;
    }

    @Override
    public void initBasic(Bundle savedInstanceState)  {
        initView();
        initLogic();
        requestData();
    }

    private void requestData() {
        SPUtil.loadArray(this,recentSearchList,"recent_search");
        if(recentSearchList.isEmpty()){
            noSearch.setVisibility(View.VISIBLE);
            ivDeleteRecentSearch.setVisibility(View.GONE);
        }else{
            noSearch.setVisibility(View.GONE);
            ivDeleteRecentSearch.setVisibility(View.VISIBLE);
            recentSearchAdapter.notifyDataSetChanged();
        }
        Map<String, Object> param = NetTool.newParams();
        NetTool.getApi().gethotsearch(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<String>>>() {
                    @Override
                    public void onData(BaseResponse<List<String>> value) {
                        hotSearchList.addAll(value.getData());
                        hotSearchAdapter.notifyDataSetChanged();
                    }
                });
    }

    private void initLogic() {
        hotSearchAdapter = new MyAdapter<String>(hotSearchList,R.layout.search_key_item) {
            @Override
            public void bindView(ViewHolder holder, final String obj) {
                holder.setText(R.id.tv_search_key,obj);
                holder.getItemView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ToastUtil.showToast("hot search"+obj);
                        startSearch(obj);
                    }
                });
            }
        };
        recentSearchAdapter = new MyAdapter<String>(recentSearchList,R.layout.search_key_item) {
            @Override
            public void bindView(ViewHolder holder, final String obj) {
                holder.setText(R.id.tv_search_key,obj);
                holder.getItemView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ToastUtil.showToast("hot search"+obj);
                        startSearch(obj);
                    }
                });
            }
        };
        gvRecentContainer.setAdapter(recentSearchAdapter);
        gvHotContainer.setAdapter(hotSearchAdapter);


        ivback.setOnClickListener(this);
        ivSearch.setOnClickListener(this);
        ivDeleteRecentSearch.setOnClickListener(this);
        tvSearchType.setOnClickListener(this);
    }

    private void initView() {
        ivback = (ImageView) findViewById(R.id.ic_back);
        ivSearch = (ImageView) findViewById(R.id.ic_search);
        ivDeleteRecentSearch = (ImageView) findViewById(R.id.iv_delete);

        tvSearchType = (TextView) findViewById(R.id.tv_search_type);
        noSearch = (TextView) findViewById(R.id.tv_no_search_record);
        etSearch = (EditText) findViewById(R.id.et_search);

        gvHotContainer = (NoScrollView) findViewById(R.id.gv_hot_search_container);
        gvRecentContainer = (NoScrollView) findViewById(R.id.gv_recent_search_container);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.ic_back:
                finish();
                break;
            case R.id.ic_search:
                startSearch("");
                break;
            case R.id.iv_delete:
                deleteHistory();
                break;
            case R.id.tv_search_type:
                if(searchType ==1 ){
                    searchType = 2;
                    tvSearchType.setText("店铺");
                }else{
                    searchType = 1;
                    tvSearchType.setText("商品");
                }
                break;

        }
    }

    private void deleteHistory() {
        SPUtil.deleteArray(this,"recent_search");
        recentSearchList.clear();
        recentSearchAdapter.notifyDataSetChanged();
        noSearch.setVisibility(View.VISIBLE);
        ivDeleteRecentSearch.setVisibility(View.GONE);
        ToastUtil.showToast("删除搜索历史记录");
    }

    private void startSearch(String s) {
        String searchStr;
        if(TextUtils.isEmpty(s)){
            searchStr = etSearch.getText().toString();
        }else{
            searchStr = s;
        }
        if(TextUtils.isEmpty(searchStr)){
            ToastUtil.showToast("输入点什么在搜索吧");
        }else{
            if(searchStr != s){
                recentSearchList.add(searchStr);
                recentSearchAdapter.notifyDataSetChanged();
                SPUtil.saveArray(this,recentSearchList,"recent_search");
            }
            ToastUtil.showToast("开始搜搜"+searchStr);
            if(searchType == 1){
                startActivity(new Intent(this,ProductListActivity.class));
            } else if (searchType == 2) {
            startActivity(new Intent(this,ShopListActivity.class));
        }
    }
    }
}
